package com.zshan.clinic.common.util.codec;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class CodecUtils {

    private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();

    // Base64编码
    public static String base64Encode(byte[] bytes) {
        return Base64.getEncoder().encodeToString(bytes);
    }

    // Base64解码
    public static byte[] base64Decode(String base64Str) {
        return Base64.getDecoder().decode(base64Str);
    }

    // Hex编码，byte数组转十六进制字符串
    public static String hexEncode(byte[] bytes) {
        char[] hexChars = new char[bytes.length * 2];
        for (int j = 0; j < bytes.length; j++) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = HEX_ARRAY[v >>> 4];
            hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
        }
        return new String(hexChars);
    }

    // Hex解码，十六进制字符串转byte数组
    public static byte[] hexDecode(String hexStr) {
        int len = hexStr.length();
        if (len % 2 != 0) {
            throw new IllegalArgumentException("Invalid hex string length");
        }
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            int high = Character.digit(hexStr.charAt(i), 16);
            int low = Character.digit(hexStr.charAt(i + 1), 16);
            if (high == -1 || low == -1) {
                throw new IllegalArgumentException("Invalid hex character");
            }
            data[i / 2] = (byte) ((high << 4) + low);
        }
        return data;
    }

    // URL编码，默认UTF-8
    public static String urlEncode(String input) {
        try {
            return URLEncoder.encode(input, StandardCharsets.UTF_8.name());
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("UTF-8 encoding not supported", e);
        }
    }

    // URL解码，默认UTF-8
    public static String urlDecode(String input) {
        try {
            return URLDecoder.decode(input, StandardCharsets.UTF_8.name());
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("UTF-8 decoding not supported", e);
        }
    }

    public static void main(String[] args) {
        String original = "Hello, 编码解码!";

        // Base64
        String base64 = CodecUtils.base64Encode(original.getBytes(StandardCharsets.UTF_8));
        System.out.println("Base64 Encoded: " + base64);
        System.out.println("Base64 Decoded: " + new String(CodecUtils.base64Decode(base64), StandardCharsets.UTF_8));

        // Hex
        String hex = CodecUtils.hexEncode(original.getBytes(StandardCharsets.UTF_8));
        System.out.println("Hex Encoded: " + hex);
        System.out.println("Hex Decoded: " + new String(CodecUtils.hexDecode(hex), StandardCharsets.UTF_8));

        // URL编码
        String urlEncoded = CodecUtils.urlEncode(original);
        System.out.println("URL Encoded: " + urlEncoded);
        System.out.println("URL Decoded: " + CodecUtils.urlDecode(urlEncoded));
    }

}
